
class ProductDetailsModel {
  int? id;
  String? imageUrl;
  String? skuName;
  String? title;
  num? amount;
  num? originalAmount;
  int stock;
  List<String> carouselUrlList;
  List<ProductSpec> specInfo;

  ProductDetailsModel({
    required this.id,
    required this.imageUrl,
    required this.skuName,
    required this.title,
    required this.amount,
    required this.originalAmount,
    required this.stock,
    required this.carouselUrlList,
    required this.specInfo,
  });
}

class ProductSpuDetailModel {
  int? id;
  String? productName;
  String? introduction;
  String? imageUrl;
  List<ProductSkuSpec> specInfo;

  ProductSpuDetailModel({this.id, this.productName, this.introduction, this.imageUrl, required this.specInfo});
}

class ProductSkuDetailModel {
  int? id;
  String? skuName;
  int? salePrice;
  int? marketPrice;
  String? imgUrl;
  int? saleStock;
  List<ProductSkuSpec>? specInfo;

  ProductSkuDetailModel({
    this.id,
    this.skuName,
    this.salePrice,
    this.marketPrice,
    this.imgUrl,
    this.saleStock,
    this.specInfo});

}

class ProductSkuSpec {
  String? title;
  List<SpecValue> buttons;

  ProductSkuSpec({this.title, required this.buttons});
}

class SpecValue {
  String text;
  String skuNo;
  List<String> skuList;

  SpecValue({required this.text, required this.skuNo, required this.skuList});

}

class ProductSpec {
  String specName;
  String specValue;

  ProductSpec({
    required this.specName,
    required this.specValue,
  });
}

List<ProductSpec> specList = [
  ProductSpec(specName: "颜色", specValue: "白色"),
  ProductSpec(specName: "版本", specValue: "8G+128GB"),
  ProductSpec(specName: "可选版本", specValue: "官方标配"),
];

List<ProductSpec> specList1 = [
  ProductSpec(specName: "颜色", specValue: "黄色"),
  ProductSpec(specName: "版本", specValue: "8G+256GB"),
  ProductSpec(specName: "可选版本", specValue: "官方标配"),
];

List<ProductSpec> specList2 = [
  ProductSpec(specName: "颜色", specValue: "红色"),
  ProductSpec(specName: "尺码", specValue: "M"),
  ProductSpec(specName: "可选版本", specValue: "官方标配"),
];


ProductSpuDetailModel spuDetailModel = ProductSpuDetailModel(id:1, productName: "浅色米黄长裤", introduction: "浅色米黄长裤浅色米黄长裤", imageUrl: "https://img.js.design/assets/img/6233dbf4e03431acdc0bdab5.png", specInfo: skuSpecList);


List<ProductSkuSpec> skuSpecList = [
  ProductSkuSpec(
      title: "颜色",
      buttons: [
        SpecValue(text: "白色", skuNo: "1", skuList: [
          "59724611000",
          "59788201252",
          "62197897734",
          "62197897735"
        ]),
        SpecValue(text: "罗兰紫", skuNo: "3", skuList: [
          "59788201253",
          "59788201254",
          "62197897736",
          "62197897737"
        ]),
        SpecValue(text: "黑色", skuNo: "5", skuList: [
          "59788201255",
          "59788201256",
          "62197897738",
          "62197897739"
        ]),
      ]
  ),
  ProductSkuSpec(
      title: "版本",
      buttons: [
        SpecValue(text: "全网通(8G+128G)(5G版)", skuNo: "1", skuList: [
          "59724611000",
          "59788201253",
          "59788201255",
          "59788201257",
          "59788201259",
          "59788201261"
        ]),
        SpecValue(text: "全网通(8G+256G)(5G版)", skuNo: "3", skuList: [
          "59788201252",
          "59788201254",
          "59788201256",
          "59788201258",
          "59788201260",
          "59788201262"
        ]),
        SpecValue(text: "全网通(6G+128G)(4G版)", skuNo: "5", skuList: [
          "62197897734",
          "62197897736",
          "62197897738",
          "62197897740",
          "62197897742",
          "62197897744"
        ]),
        SpecValue(text: "全网通(6G+512G)(4G版)", skuNo: "6", skuList: [
          "1111111",
        ]),
      ]
  ),
];

List<ProductSpuDetailModel> spuList = [
ProductSpuDetailModel(id:1, productName: "浅色米黄长裤", introduction: "浅色米黄长裤浅色米黄长裤", imageUrl: "https://img.js.design/assets/img/6233dbf4e03431acdc0bdab5.png", specInfo: skuSpecList),
];







List<ProductDetailsModel> productList = [
  ProductDetailsModel(id: 1, imageUrl: "https://img.js.design/assets/img/6233dbf4e03431acdc0bdab5.png", skuName: " 浅色米黄长裤", title: "小果冻长方形粉底液化妆棉干湿两用", amount: 160.1, originalAmount: 180.88, stock: 10,
      carouselUrlList: ["https://img.js.design/assets/img/6233dbf4e03431acdc0bdab5.png", "https://img.js.design/assets/img/6233dbf4e03431acdc0bdab5.png"], specInfo: specList),
  ProductDetailsModel(id: 2, imageUrl: "https://img.js.design/assets/img/6233dbf4e03431acdc0bdab5.png", skuName: " 浅色米黄长裤1", title: "小果冻长方形粉底液化妆棉干湿两用", amount: 161.1, originalAmount: 181.88, stock: 10,
      carouselUrlList: ["https://img.js.design/assets/img/6233dbf4e03431acdc0bdab5.png", "https://img.js.design/assets/img/6233dbf4e03431acdc0bdab5.png"], specInfo: specList1),
  ProductDetailsModel(id: 3, imageUrl: "https://img.js.design/assets/img/6233dbf4e03431acdc0bdab5.png", skuName: " 浅色米黄长裤2", title: "小果冻长方形粉底液化妆棉干湿两用", amount: 162.1, originalAmount: 182.88, stock: 10,
      carouselUrlList: ["https://img.js.design/assets/img/6233dbf4e03431acdc0bdab5.png", "https://img.js.design/assets/img/6233dbf4e03431acdc0bdab5.png"], specInfo: specList1),
  ProductDetailsModel(id: 4, imageUrl: "https://img.js.design/assets/img/6233dbf4e03431acdc0bdab5.png", skuName: " 浅色米黄长裤3", title: "小果冻长方形粉底液化妆棉干湿两用", amount: 163.1, originalAmount: 183.88, stock: 10,
      carouselUrlList: ["https://img.js.design/assets/img/6233dbf4e03431acdc0bdab5.png", "https://img.js.design/assets/img/6233dbf4e03431acdc0bdab5.png"], specInfo: specList2),
  ProductDetailsModel(id: 5, imageUrl: "https://img.js.design/assets/img/6233dbf4e03431acdc0bdab5.png", skuName: " 浅色米黄长裤4", title: "小果冻长方形粉底液化妆棉干湿两用", amount: 164.1, originalAmount: 184.88, stock: 10,
      carouselUrlList: ["https://img.js.design/assets/img/6233dbf4e03431acdc0bdab5.png", "https://img.js.design/assets/img/6233dbf4e03431acdc0bdab5.png"], specInfo: specList2),
  ProductDetailsModel(id: 6, imageUrl: "https://img.js.design/assets/img/6233dbf4e03431acdc0bdab5.png", skuName: " 浅色米黄长裤5", title: "小果冻长方形粉底液化妆棉干湿两用", amount: 165.1, originalAmount: 185.88, stock: 10,
      carouselUrlList: ["https://img.js.design/assets/img/6233dbf4e03431acdc0bdab5.png", "https://img.js.design/assets/img/6233dbf4e03431acdc0bdab5.png"], specInfo: specList2),
  ProductDetailsModel(id: 7, imageUrl: "https://img.js.design/assets/img/6233dbf4e03431acdc0bdab5.png", skuName: " 浅色米黄长裤6", title: "小果冻长方形粉底液化妆棉干湿两用", amount: 166.1, originalAmount: 186.88, stock: 10,
      carouselUrlList: ["https://img.js.design/assets/img/6233dbf4e03431acdc0bdab5.png", "https://img.js.design/assets/img/6233dbf4e03431acdc0bdab5.png"], specInfo: specList2),
  ProductDetailsModel(id: 8, imageUrl: "https://img.js.design/assets/img/6233dbf4e03431acdc0bdab5.png", skuName: " 浅色米黄长裤7", title: "小果冻长方形粉底液化妆棉干湿两用", amount: 167.1, originalAmount: 187.88, stock: 10,
      carouselUrlList: ["https://img.js.design/assets/img/6233dbf4e03431acdc0bdab5.png", "https://img.js.design/assets/img/6233dbf4e03431acdc0bdab5.png"], specInfo: specList2),
  ProductDetailsModel(id: 9, imageUrl: "https://img.js.design/assets/img/6233dbf4e03431acdc0bdab5.png", skuName: " 浅色米黄长裤8", title: "小果冻长方形粉底液化妆棉干湿两用", amount: 168.1, originalAmount: 188.88, stock: 10,
      carouselUrlList: ["https://img.js.design/assets/img/6233dbf4e03431acdc0bdab5.png", "https://img.js.design/assets/img/6233dbf4e03431acdc0bdab5.png"], specInfo: specList2),
  ProductDetailsModel(id: 10, imageUrl: "https://img.js.design/assets/img/6233dbf4e03431acdc0bdab5.png", skuName: " 浅色米黄长裤9", title: "小果冻长方形粉底液化妆棉干湿两用", amount: 169.1, originalAmount: 189.88, stock: 10,
      carouselUrlList: ["https://img.js.design/assets/img/6233dbf4e03431acdc0bdab5.png", "https://img.js.design/assets/img/6233dbf4e03431acdc0bdab5.png"], specInfo: specList2),
];

class SpecSkuUtils {
  static Map<String, List<String>> getSpecList (List<ProductDetailsModel> skuList) {
    Map<String, List<String>> skuSpecMap = new Map<String, List<String>>();
    for (var sku in skuList) {
      for (var specInfo in sku.specInfo) {
        if (skuSpecMap.containsKey(specInfo.specName)) {
          List<String> specValue = skuSpecMap[specInfo.specName] ?? [];
          if (specValue.contains(specInfo.specValue)) {
            continue;
          }
          specValue.add(specInfo.specValue);
          skuSpecMap[specInfo.specName] = specValue;
        } else {
          List<String> specValue = [specInfo.specValue];
          skuSpecMap.addAll({specInfo.specName : specValue});
        }
      }
    }
    return skuSpecMap;
  }

  static Map<String, ProductDetailsModel> initSpecList (List<ProductDetailsModel> skuList) {
    Map<String, ProductDetailsModel> skuSpecMap = new Map<String, ProductDetailsModel>();
    for (var sku in skuList) {
      List<ProductSpec> specInfo = sku.specInfo;
      var specList = [];
      for (var specStr in specInfo) {
        specList.add(specStr.specValue);
      }
      skuSpecMap.addAll({specList.join(";") : sku});
    }
    return skuSpecMap;
  }

  static Map<String, ProductDetailsModel> match(Map<String, ProductDetailsModel> skuMap, String selectValue) {
    if (selectValue == '') {
      return skuMap;
    }
    Map<String, ProductDetailsModel> skuSpecMap = new Map<String, ProductDetailsModel>();
    skuMap.forEach((String key, ProductDetailsModel value) {
      if (key.contains(selectValue)) {
        skuSpecMap[key] = value;
      }
    });
    return skuSpecMap;
  }

}

main() {
  print(SpecSkuUtils.initSpecList(productList));
}